﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaxiCaosSOAPService.Dominio;
using System.Data.SqlClient;
using System.Data;
using NHibernate;

namespace TaxiCaosSOAPService.Persistencia
{
    public class ReservaDAO : BaseDAO<Reserva, int>
    {
        public string ObtenerSiguienteNroTicket()
        {
            string ticket = "";
            string sql = "select ISNULL(MAX(convert(INTEGER,r.Ticket)),0)+1 as Ticket from reserva r";
            using (SqlConnection con = new SqlConnection(ConexionUtil.ObtenerConexion()))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    using (SqlDataReader resultado = com.ExecuteReader())
                    {
                        if (resultado.Read())
                        {
                            ticket = resultado["Ticket"].ToString();
                        }

                    }

                }
            }

            return ticket.PadLeft(10, '0');
        }

        public int ObtenerCantidadReservas(int idcliente)
        {
            using (ISession sesion = NHibernateHelper.ObtenerSesion())
            {
                string sentenciaSQL = "select d1 from Reserva d1 where d1.IdCliente = :IdCliente";
                IQuery query = sesion.CreateQuery(sentenciaSQL);
                query.SetParameter("IdCliente", idcliente);
                IList<Reserva> lista = query.List<Reserva>();
                
                return lista.Count;
            }
        }
    }
}